package cn.chenxiejia;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/***
 * @program: maven
 * @ClassName: File3
 * @Description: 使用fileinputstream读取文件内容
 * @Author: chenxiejia
 * @Date: 2022/1/17 22:01
 * @version : V1.0
 */
public class File3 {
    public static void main1(String[] args) throws IOException {
        //不可自动创建文件夹,append开启追加
        FileOutputStream file = new FileOutputStream("E:/aa.txt", true);
        file.write("Hello world!\n中文效果\n".getBytes());
        file.write("Hello world!\n中文效果\n".getBytes());
        file.write("Hello world!\n中文效果\n".getBytes());
        file.close();

        FileInputStream file2 = new FileInputStream("E:/aa.txt");
        //数据中转站 临时缓冲区
        byte[] buf = new byte[1024];
        int len = 0;
        //循环读取文件内容，输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
        while ((len = file2.read(buf)) > 0) {
            System.out.println(new String(buf, StandardCharsets.UTF_8));
        }
        file2.close();

    }

    public static void main(String[] args) {
        try (FileInputStream fs = new FileInputStream("E:/aa.txt")) {
            byte[] v = fs.readAllBytes();
            System.out.println(new String(v));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
